﻿<%@ WebHandler Language="C#" Class="TestHandler" %>

using System;
using System.Web;
using System.Web.SessionState;
using Common;
using Model;
using System.Collections.Generic;
using System.Linq;
using Service;
using Cache.Redis;
using Model.User;
using Model.Port;
using DataService.Port;
using Service.Table;
using MySql.Data.MySqlClient;
using System.Data;

public class TestHandler : IHttpHandler, IRequiresSessionState
{
    private HttpContext context;
    public void ProcessRequest(HttpContext context)
    {
        this.context = context;
        context.Response.ContentType = "text/plain";
        string result = string.Empty;
        string method = context.Request["method"];
        switch (method)
        {
            case "AddItemToSortedSet":
                AddItemToSortedSet();
                break;
            case "GetCaptainIcon":
                result = GetCaptainIcon();
                break;
            case "test":
                result = test();
                break;
            case "IsSame":
                result = IsSame();
                break;
            case "GroupBytest":
                result = GroupBytest();
                break;
            case "testtableshiplv":
                result = testtableshiplv();
                break;
            case "TestList":
                result = TestList();
                break;
            case "TestSeconds":
                result = TestSeconds();
                break;
            case "GetObjectList":
                result = GetObjectList();
                break;
            case "GetErrorLog":
                result = GetErrorLog();
                break;
            case "GetThrow":
                result = GetThrow();
                break;
            case "GetInterfaceName":
                result = GetInterfaceName();
                break;
            case "TestExpire":
                TestExpire();
                break;
            default: break;
        }
        context.Response.Write(result);
    }

    public string GetInterfaceName()
    {
        ServiceConfig config = new ServiceConfig();
        IQueryInterface service = config.GetQueryService(1102);    //根据command_id，调用对应的业务逻辑模块进行处理 1102
        return service.GetType().Name;
    }

    public void TestExpire()
    {
        RedisHelper.instance.AddItemToSortedSet("testtest", "test");
        RedisHelper.instance.ExpireEntry("testtest", new TimeSpan(0, 1, 0));
    }

    public string GetThrow()
    {
        string result = string.Empty;
        try
        {
            ThrowError();
        }
        catch (Exception er)
        {
            result = er.Message + "\n" + er.StackTrace + "\n" + er.ToString();
        }
        return result;
    }

    public void ThrowError()
    {
        try
        {
            string a = "test";
            int b = int.Parse(a);
        }
        catch (Exception ex)
        {
            throw new Exception("ThrowError", ex);
        }
        //throw new Exception("seahelper", new Exception("table is null"));
    }

    public string GetErrorLog()
    {
        string id = context.Request["Id"];
        string sqlStr = @"select Message from errorlog where Id = ?Id";
        MySqlParameter[] param = new MySqlParameter[] { 
            new MySqlParameter("?Id", id)
        };
        var ret = MySQLDataControl.ExecuteScalar(MySQLDataControl.GetConnection("MySqlLogConn"), CommandType.Text, sqlStr, param);
        return ret != null ? ret.ToString() : "";
    }

    public string GetObjectList()
    {
        List<Ship> shipList = DataBusiness.GetObjectList<Ship>("211");
        return CheckInfo.JsonSerializer(shipList);
    }

    public string TestSeconds()
    {
        long fixstarttime = 1542178265;
        int fixtime = 2400;
        DateTime endTime = CheckInfo.ConvertUTCTimeStampToDateTime(fixstarttime + fixtime);
        DateTime dateNow = DateTime.Now;
        string result = Math.Ceiling((endTime - dateNow).TotalSeconds).ToString();
        return result;
    }

    public string TestList()
    {
        var i = 0;
        List<int> list = new List<int>();
        foreach (var item in list)
        {
            i = i + 1;
        }
        return i.ToString();
    }

    public string testtableshiplv()
    {
        List<TableShipLvUp> table_ship_lv = TableService.GetTableObjListById<TableShipLvUp>("1");

        return CheckInfo.JsonSerializer(table_ship_lv);
    }

    public string GroupBytest()
    {
        List<KeyValuePair<string, string>> valueList = new List<KeyValuePair<string, string>>();

        List<Dictionary<string, object>> dicList = new List<Dictionary<string, object>>();
        Dictionary<string, object> dic1 = new Dictionary<string, object>();
        dic1.Add("id", 1);
        dic1.Add("ship_id", 1);
        dic1.Add("name", "test11");
        Dictionary<string, object> dic2 = new Dictionary<string, object>();
        dic2.Add("id", 2);
        dic2.Add("ship_id", 1);
        dic2.Add("name", "aaaaa");
        Dictionary<string, object> dic3 = new Dictionary<string, object>();
        dic3.Add("id", 3);
        dic3.Add("ship_id", 2);
        dic3.Add("name", "yayayay");

        dicList.Add(dic1);
        dicList.Add(dic2);
        dicList.Add(dic3);
        var retList = dicList.GroupBy(x => x["ship_id"]).ToList();
        foreach (var item in retList)
        {
            valueList.Add(new KeyValuePair<string, string>(item.Key.ToString(), MiniJSON.Json.Serialize(item.ToList())));
        }
        return CheckInfo.JsonSerializer(valueList);
    }

    public string IsSame()
    {
        List<int> old_ship_id_list = new List<int>() { 2 };
        List<int> new_ship_id_list = new List<int>() { 1, 2 };
        bool isSame = old_ship_id_list.All(new_ship_id_list.Contains) && old_ship_id_list.Count == new_ship_id_list.Count;
        return isSame ? "true" : "false";
    }

    public string test()
    {
        //CaptainData captain_data = new CaptainData();
        //captain_data.CaptainList = new List<Captain>();
        //captain_data.CaptainList.Add(new Captain() { Id = 1, Name = "11" });
        //captain_data.CaptainList.Add(new Captain() { Id = 2, Name = "22" });
        //captain_data.CaptainList.Add(new Captain() { Id = 3, Name = "33" });

        //Captain cap = captain_data.CaptainList.Where(p => p.Id == 2).FirstOrDefault();
        //cap.Name = "test";
        //return CheckInfo.JsonSerializer(captain_data);
        TableService.GetTableObjectList<TableCommonProperty>(); //入口处统一获取公共属性配置
        var ret = TableCommonProperty.init_ship;
        return CheckInfo.JsonSerializer(ret);
    }

    public string GetCaptainIcon()
    {
        string name = CaptainInfo.GetCaptainName();
        string icon = "";
        var list = CaptainInfo.GetCaptainSkill();
        return name + " \n " + icon;
    }

    public string random()
    {
        int skill_count_random = new Random().Next(1, 3);
        return skill_count_random.ToString();
    }

    public void AddItemToSortedSet()
    {
        RedisConnect conn = new RedisConnect("RedisWriteServerLocal", 0);
        bool resul1 = conn.AddItemToSortedSet("sortedsettest", "11");
        bool resul2 = conn.AddItemToSortedSet("sortedsettest", "22");
        bool resul3 = conn.AddItemToSortedSet("sortedsettest", "11");
        bool resul4 = conn.AddItemToSortedSet("sortedsettest", "11");
        bool resul5 = conn.AddItemToSortedSet("sortedsettest", "33");
    }

    public string GetFloat()
    {
        float a = 5f;
        float b = a / 3;
        return b.ToString();
    }

    //public string RedisSet()
    //{
    //    RedisConnect redis = new RedisConnect();

    //}

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}